<?php

$app_id = '';   //商户APP ID（后台接口管理页面获取）
$app_key = '';  //商户APP KEY(后台接口管理页面获取)
$pay_type = ''; //商户支付方式(通过运营获取)

$url = 'http://47.75.17.153/pay/order';
$ret = test_order($app_id, $app_key, $pay_type, $url);
var_dump($ret);

/**
 初始化订单
 */
function test_order($app_id, $app_key, $pay_type, $url, $notify_url = '', $return_url = '')
{
    $order_amt = '10.00'; //支付金额
    $order_id = time() . 'order'; //订单号
    $goods_name = '物品名称'; //物品名称
    $goods_note = ''; //物品名称
    $extends = '扩展字段'; //物品名称
    $time_stamp = date('YmdHis', time()); //时间戳

    $sign = 'app_id='.$app_id
        .'&pay_type='.$pay_type
        .'&order_id='.$order_id
        .'&order_amt='.$order_amt
        .'&notify_url='.$notify_url
        .'&return_url='.$return_url
        .'&time_stamp='.$time_stamp
        .'&key='.$app_key;

    $param = array(
        'app_id' => $app_id,
        'pay_type' => $pay_type,
        'order_id' => $order_id,
        'order_amt' => $order_amt,
        'notify_url' => urlencode($notify_url),
        'return_url' => urlencode($return_url),
        'goods_name' => urlencode($goods_name),
        'goods_note' => urlencode($goods_note),
        'extends' => urlencode($extends),
        'user_ip' => '127.0.0.1',
        'time_stamp' => $time_stamp,
        'sign' => md5($sign),
    );

    $ret = send_post($url, $param);
    return $ret;
}

function send_post($url, $param)
{
    $postdata = http_build_query($param);
    $options = array(
        'http' => array(
            'method' => 'POST',
            'header' => 'Content-type:application/x-www-form-urlencoded',
            'content' => $postdata,
            'timeout' => 15 * 60, // 超时时间（单位:s）
        ),
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);

    return $result;
}